<template>
  <div class="system-log-container">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="消息类型" prop="message_type">
        <el-select v-model="form.message_type" placeholder="请选择消息类型">
          <el-option
            v-for="item in list"
            :key="item.item_code"
            :label="item.item_name"
            :value="item.item_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收方" prop="accept_uid">
        <el-select v-model="form.accept_uid" placeholder="请选择接收方">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.user_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消息标题" prop="message_title">
        <el-input v-model="form.message_title" />
      </el-form-item>
      <el-form-item label="消息内容" prop="message_content">
        <el-input v-model="form.message_content" rows="5" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">发 送</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { addMessage } from '@/api/message'
  import { queryList } from '@/api/userManagement'
  import { getDictByCode } from '@/utils/dict'
  import { mapActions } from 'vuex'
  export default {
    name: 'SendMsg',
    data() {
      return {
        form: {
          message_title: '',
          message_content: '',
        },
        rules: {
          message_type: [
            { required: true, trigger: 'blur', message: '请选择消息类型' },
          ],
          accept_uid: [
            { required: true, trigger: 'blur', message: '请选择接收方' },
          ],
          message_title: [
            { required: true, trigger: 'blur', message: '请输入消息标题' },
          ],
          message_content: [
            { required: true, trigger: 'blur', message: '请输入消息内容' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
        userList: [],
      }
    },
    created() {
      this.getDict()
      this.getUserList()
    },
    methods: {
      ...mapActions({
        getView: 'user/getView',
      }),
      async getDict() {
        this.list = await getDictByCode('message_type')
      },
      async getUserList() {
        const { data } = await queryList({})
        this.userList = data
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form)
            const { data } = await addMessage(this.form)
            if (data.is_success) {
              this.$baseMessage(
                '发送成功',
                'success',
                'vab-hey-message-success'
              )
            }
            this.getView()
            this.$refs['form'].resetFields()
            this.form = this.$options.data().form
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-input {
    width: 215px;
  }
  .el-textarea {
    width: 400px;
  }
</style>
